// proxy/ProxyUser.java
package proxy;

import book.Library;
import constant.Constant;
import user.AdminUser;
import user.NormalUser;
import user.User;
import utils.LibrarySingleton;

public class ProxyUser {
    private User realUser;
    private Library library;

    public ProxyUser(User user) {
        this.realUser = user;
        this.library = LibrarySingleton.getInstance();
    }

    public User getRealUser() {
        return realUser;
    }

    private void checkRealUserWhetherAdminUser(String message) {
        if (!(realUser instanceof AdminUser)) {
            throw new RuntimeException(message);
        }
    }

    private void checkRealUserWhetherNormalUser(String message) {
        if (!(realUser instanceof NormalUser)) {
            throw new RuntimeException(message);
        }
    }

    public void addBook() {
        checkRealUserWhetherAdminUser("普通用户没有权限上架图书");
        ((AdminUser) realUser).addBook();
    }

    public void updateBook() {
        checkRealUserWhetherAdminUser("普通用户没有权限更新图书");
        ((AdminUser) realUser).updateBook();
    }

    public void removeBook() {
        checkRealUserWhetherAdminUser("普通用户没有权限删除图书");
        ((AdminUser) realUser).removeBook();
    }

    public void borrowCount() {
        checkRealUserWhetherAdminUser("普通用户没有权限查看图书的借阅次数");
        ((AdminUser) realUser).borrowCount();
    }

    public void generateBook() {
        checkRealUserWhetherAdminUser("普通用户没有权限查看最受欢迎的前k本书");
        ((AdminUser) realUser).generateBook();
    }

    public void checkInventoryStatus() {
        checkRealUserWhetherAdminUser("普通用户没有权限查看库存状态");
        ((AdminUser) realUser).checkInventoryStatus();
    }

    public void categorizeBooksByCategory() {
        checkRealUserWhetherAdminUser("普通用户没有权限查看按照类别统计图书");
        ((AdminUser) realUser).categorizeBooksByCategory();
    }

    public void categorizeBooksByAuthor() {
        checkRealUserWhetherAdminUser("普通用户没有权限查看按照作者统计图书");
        ((AdminUser) realUser).categorizeBooksByAuthor();
    }

    public void checkAndRemoveOldBooks() {
        checkRealUserWhetherAdminUser("普通用户没有权限移除上架超过一年的图书");
        ((AdminUser) realUser).checkAndRemoveOldBooks();
    }

    public void borrowBook() {
        checkRealUserWhetherNormalUser("管理员请以普通用户的方式借阅图书");
        ((NormalUser) realUser).borrowBook();
    }

    public void returnBook() {
        checkRealUserWhetherNormalUser("管理员请以普通用户的方式归还图书");
        ((NormalUser) realUser).returnBook();
    }

    public void viewBorrowHistory() {
        checkRealUserWhetherNormalUser("管理员请以普通用户的方式查看个人当前借阅情况");
        ((NormalUser) realUser).viewBorrowBooks();
    }

    public void handleOperation(int choice) {
        if (realUser instanceof AdminUser) {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    borrowCount();
                    break;
                case Constant